<header>
    <div id="header">
        <div class="logo"><a href="home.html"><img src="/web/images/logo.png"></a></div>
<!--        <div class="rg_btn"><a href="javascript:;"><img src="/web/images/menu.png"></a></div>-->
        <nav>
            <ul>
                <li class="Collapsing" id="1"><a href="home.html">企业首页</a></li>
                <li id="2" class="Collapsing"><span>业务管理</span></li>
                <div class="coll_body">
                    <li id="11"><a href="consulting.html">咨询线索</a></li>
                    <li id="12"><a href="customer-files.html">客户档案</a></li>
                    <li id="13"><a href="customer-tracking.html">客户跟进</a></li>
                    <li id="14"><a href="offer.html">项目报价</a></li>
                    <li id="17"><a href="business-contract.html">业务合同</a></li>
                    <li id="15"><a href="business-credit.html">业务收款</a></li>
                    <li id="16"><a href="invoice-management.html">发票管理</a></li>
                </div>
                <li class="Collapsing"><span>项目任务管理</span></li>
                <li class="Collapsing"><span>业绩奖金</span></li>
                <li class="Collapsing"><span>人事资料</span></li>
                <li class="Collapsing"><span>行政服务</span></li>
                <li class="Collapsing"><span>公告</span></li>
                <li class="Collapsing"><span>财务管理</span></li>
                <li class="Collapsing"><span>供应产品成本</span></li>
                <li id="10" class="Collapsing"><span>系统设置</span></li>
                <div class="coll_body">
                    <li id="60"><a href="userlist.html">用户列表</a></li>
                    <li id="61"><a href="department.html">部门列表</a></li>
                    <li id="62"><a href="parameter.html">参数配置</a></li>
                </div>
            </ul>
        </nav>
    </div>
    <script src="js/jquery-1.10.0.min.js"></script>
<!--    <script src="js/transform.js"></script>-->
    <script>
        headerData = new Vue({
            el: '#header',
            data: {
                titles: [],
                passid: 0
            },
            methods: {
                search(e, table, field) {
                    var that = this;
                    var keywords = e.target.value;
                    $.ajax({
                        type: "GET",
                        dataType: "json",
                        url: '/keyword',
                        data: {'table': table, 'field': field, 'keywords': keywords},
                        success: function (data) {
                            headerData.verification(data);
                            AppData.tableDataList = data.list;
                        },
                        error: function (msg) {
                            console.log(msg);
                        }
                    });
                },
                //最重要的一部
                exportPathMethod(data, name) {
                    // 要导出的json数据
                    var jsonData = data
                    let str1 = ''
                    for (const i in jsonData[0]) {
                        str1 += ',' + i
                    }
                    str1 = str1.substring(1)
                    str1 = str1 + '\n'
                    let str = `${str1}`
                    // 列标题，逗号隔开，每一个逗号就是隔开一个单元格
                    // let str = `用户名称,会员等级,最近购买时间,消费次数,消费金额,用户账号,订单均价\n`
                    // 增加\t为了不让表格显示科学计数法或者其他格式
                    for (let i = 0; i < jsonData.length; i++) {
                        for (const item in jsonData[i]) {
                            str += `${jsonData[i][item] + '\t'},`
                        }
                        str += '\n'
                    }

                    // encodeURIComponent解决中文乱码
                    const uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str)
                    // 通过创建a标签实现
                    var link = document.createElement('a')
                    link.href = uri
                    // 对下载的文件命名
                    link.download = name + '.xls'
                    document.body.appendChild(link)
                    link.click()
                },
                excelDown: function (type, data) {
                    var self = this;
                    self.fileExcelIdString = "";
                    if (type === 2) {
                        if (!(self.selectedFileData.length > 0)) {
                            return self.$message({
                                type: 'info',
                                message: '请先选择数据!'
                            });
                        }
                        self.selectedFileData.forEach(function (item, index) {
                            self.fileExcelIdString = self.fileExcelIdString + item.fileId + ","
                            self.excelDownStart(item.fileName, item.fileId);
                        });
                        self.fileExcelIdString = self.fileExcelIdString.substr(0, self.fileExcelIdString.length - 1);
                    }
                    ;
                    if (type === 1) {
                        self.fileExcelIdString = self.fileExcelIdString + data.fileId;
                        self.excelDownStart(data.fileName, data.fileId)
                    }
                },
                excelDownStart: function (fileName, fileId) {
                    var url = _contextPath + "/dataps/inter/download/" + fileId;//根据ID下载地址
                    var userAgent = navigator.userAgent.toLowerCase();
                    var isIe = /(msie|trident).*?([\d.]+)/.test(userAgent);
                    if (isIe) { // IE
                        window.open(url, '_blank')
                    } else {
                        var a = document.createElement('a'); // 创建a标签
                        var e = document.createEvent('MouseEvents'); // 创建鼠标事件对象
                        e.initEvent('click', false, false) // 初始化事件对象
                        a.href = url; // 设置下载地址
                        a.download = fileName + moment(new Date().getTime()).format('YYYY-MM-DD HH:mm:ss'); // 设置下载文件名
                        a.dispatchEvent(e);
                    }
                },
                departmentchange(id){
                    $.ajax({
                        type: "GET",
                        dataType: "json",
                        url: '/order/' + id + '/edit',
                        data: {},
                        success: function (data) {
                            headerData.verification(data);
                            AppData.user = data.user;
                            AppData.userid = 0;
                        },
                        error: function (msg) {
                            console.log(msg);
                        }
                    });
                },
                verification(data) {
                    if (data.status == 2) {
                        layer.msg(data.message, {icon: 2, time: 1000}, function () {
                            window.location.href = "login.html";
                        });
                    }
                }
            }
        })

        $.ajax({
            type: "GET",
            dataType: "json",
            url: '/index',
            data: {},
            success: function (data) {
                headerData.verification(data);
                let passid = data.userInfo.id;
                rightData.passid = passid;
                let dataS = data.userInfo.jurisdiction;//后台初始数据
                var dataArr = [];
                for (let i in dataS) {
                    dataArr.push(dataS[i]); //属性
                }
                /*调用封装的改变数据结构方法*/
                let tableData = setTreeData(dataArr);
                tableData.forEach(item => {
                    item.isSubShow = false;
                })
                // let tableDataObj = {...tableData};//把转换后的数组通过扩展运算符改成对象便于遍历
                // 在数据的每一项里面添加一个新的属性
                headerData.titles = tableData;
            },
            error: function (msg) {
                console.log(msg);
            }
        });

        //获取当前时间
        function getNow(s) {
            return s < 10 ? '0' + s : s;
        }

        var myDate = new Date();
        var year = myDate.getFullYear();        //获取当前年
        var month = myDate.getMonth() + 1;   //获取当前月
        var date = myDate.getDate();            //获取当前日
        var h = myDate.getHours();              //获取当前小时数(0-23)
        var m = myDate.getMinutes();          //获取当前分钟数(0-59)
        var s = myDate.getSeconds();
        var now = year + '-' + getNow(month) + "-" + getNow(date) + " " + getNow(h) + ':' + getNow(m) + ":" + getNow(s);
        $('.logouttime').html(now);

        //列表效果
        $(".Collapsing").click(function () {
            $(this).toggleClass("active").siblings('.Collapsing').removeClass("active");//切换图标
            $(this).next(".coll_body").slideToggle(500).siblings(".coll_body").slideUp(500);
        });

        //根据url链接判断子类是否展示
        //截取url中.html的参数
        url = window.location.pathname;
        url = url.substring(url.lastIndexOf('/') + 1, url.length);
        if (url == 'consulting.html' || url == 'customer-files.html' || url == 'customer-tracking.html' || url == 'offer.html' || url == 'business-credit.html' || url == 'invoice-management.html' || url == 'business-contract.html' || url == 'customer-details.html'){
            $(".coll_body").eq(0).show();
            $('#2').addClass('active');
        }
        else if(url == 'userlist.html' || url == 'department.html' || url == 'parameter.html'){
            $(".coll_body").eq(1).show();
            $('#10').addClass('active');
        }
        //根据url添加当前样式
        if (url == 'consulting.html'){
            $('#11').addClass('active');
        }else if(url == 'customer-files.html' || url == 'customer-details.html' ){
            $('#12').addClass('active');
        }else if(url == 'customer-tracking.html'){
            $('#13').addClass('active');
        }else if(url == 'offer.html'){
            $('#14').addClass('active');
        }else if(url == 'business-credit.html'){
            $('#15').addClass('active');
        }else if(url == 'invoice-management.html'){
            $('#16').addClass('active');
        }else if(url == 'business-contract.html'){
            $('#17').addClass('active');
        }else if(url == 'userlist.html'){
            $('#60').addClass('active');
        }else if(url == 'department.html'){
            $('#61').addClass('active');
        }else if(url == 'parameter.html'){
            $('#62').addClass('active');
        }

    </script>
</header>
